package com.otitan.la.forest.industry.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import cn.hutool.core.util.StrUtil;
import com.otitan.la.forest.industry.constant.DicEnum;
import com.otitan.la.forest.industry.entity.EnterpriseInputEntity;
import com.otitan.la.forest.industry.service.EnterpriseService;
import com.otitan.la.forest.industry.utils.CommonUtils;
import com.otitan.la.forest.industry.utils.ImOrExportUtil;
import lombok.Data;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.*;

/**
 * @Description:    龙头企业导入校验对象
 * @Author:        Sunke
 * @CreateDate:     2020/10/20 0020 11:16
 * @UpdateUser:     Sunke
 * @UpdateDate:     2020/10/20 0020 11:16
 * @UpdateRemark:   修改内容
 * @Version:        1.0
 */
@Data
public class EnterpriseVerifyHandler implements IExcelVerifyHandler<EnterpriseInputEntity> {
    private final ThreadLocal<List<EnterpriseInputEntity>> threadLocal = new ThreadLocal<>();
    private Map dicMap = new HashMap();
    private EnterpriseService enterpriseService;
    private String redisKeyPrefix;
    private StringRedisTemplate redisTemplate;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(EnterpriseInputEntity enter) {
        StringJoiner joiner = new StringJoiner(",");
        // 根据名称与手机号判断数据是否重复
        String name = enter.getName();
        String code = enter.getCode();
        //校验数据是否重复
        boolean duplicates = enterpriseService.checkForDuplicates(name, code);
        if (duplicates) {
            joiner.add("企业名称重复");
        }
        //校验字典
        Map mapArea=ImOrExportUtil.getDicName( DicEnum.AREA.getValue(),redisTemplate,redisKeyPrefix);
        String areaCode=ImOrExportUtil.mapToString(mapArea,enter.getAreaCode());
        Map mapEnter=ImOrExportUtil.getDicName( DicEnum.ENTERPRISETYPE.getValue(),redisTemplate,redisKeyPrefix);
        String enterCode=ImOrExportUtil.mapToString(mapEnter,enter.getEnterType());
        if (StrUtil.isEmpty(enterCode)) {
            joiner.add("该企业类型不存在");
        } else {

        }
        if (StrUtil.isEmpty(areaCode)) {
            joiner.add("该地区不存在");
        }

        //判断表格中数据是否重复
        List<EnterpriseInputEntity> threadLocalVal = threadLocal.get();
        if (threadLocalVal == null) {
//            //判断表格数据是否为空
//                joiner.add("表格数据为空");
            threadLocalVal = new ArrayList<>();
        }
        for(int i=0;i<threadLocalVal.size();i++){
            if(enter.getRowNum()>threadLocalVal.get(i).getRowNum() && threadLocalVal.get(i).getName().equals(enter.getName())){
                int enterRow=enter.getRowNum()+1;
                int thread=threadLocalVal.get(i).getRowNum()+1;
                joiner.add("第"+enterRow+"行数据与第" +thread+ "行数据重复");
            }
        }
        // 添加本行数据对象到ThreadLocal中
        threadLocalVal.add(enter);
        threadLocal.set(threadLocalVal);
        if (joiner.length() != 0) {
            return new ExcelVerifyHandlerResult(false, joiner.toString());
        }
        return new ExcelVerifyHandlerResult(true);
    }

    public ThreadLocal<List<EnterpriseInputEntity>> getThreadLocal() {
        return threadLocal;
    }

}
